package cn.ecut.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 浏览器过滤器，禁止IE浏览器访问，使用IE浏览器访问将回跳转找errorpage.html页面
 */

public class BrowserFilterDemo implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        response.setContentType("text/html;charset=utf-8");

        RequestDispatcher dispatcher;
        dispatcher = request.getRequestDispatcher("/errorpage.html");
        String header = request.getHeader("user-agent");

        String s = request.getServletPath();
        if (s.contains("errorpage") || !header.toLowerCase().contains("trident")) {
            filterChain.doFilter(request, response);
            return;
        }

//        response.setStatus(404);
//        response.sendError(404, "此浏览器无法访问");
        //response.sendRedirect("/errorpage.html");
        dispatcher.forward(request, response);
    }

    @Override
    public void destroy() {

    }
}
